<?php

class Kindeditor extends Font_Controller {

	protected $path = '';
	
	public function __construct()
	{
		parent::__construct();
		$this->path = FCPATH.'uploads';
	}
	
	public function upload()
	{
		$dir = $_GET['dir']; //image flash media file
		if ( ! in_array($dir, array('image', 'flash', 'media' , 'file')))
		{
			$this->alert('目录类型错误');
		}
		
		$path = $this->path.'/'.$dir.'/'.date('Y-m-d');
		
		if ( ! is_dir($path))
		{
			if ( ! mkdir($path, 0777, TRUE))
			{
				$this->alert('创建目录失败');
			}
		}
		
		$config = array(
			'upload_path' => $path
		);
		
		switch ($dir)
		{
			case 'image':
				$config['max_size'] = 500;
				$config['allowed_types'] = array('gif', 'jpg', 'jpeg', 'png', 'bmp');
				break;
			case 'flash':
				$config['max_size'] = 1024*20;
				$config['allowed_types'] = array('flv', 'swf');
				break;
			case 'media':
				$config['max_size'] = 1024*20;
				$config['allowed_types'] = array('swf', 'flv', 'mp3', 'wav', 'wma', 'wmv', 'mid', 'avi', 'mpg', 'asf', 'rm', 'rmvb');
				break;
			case 'file':
				$config['max_size'] = 1024*20;
				$config['allowed_types'] = array('doc', 'docx', 'xls', 'xlsx', 'ppt', 'htm', 'html', 'txt', 'zip', 'rar', 'gz', 'bz2');
				break;
		}
		
		$this->load->library('upload');
		
		$this->upload->config($config);
		
		if (FALSE == $this->upload->do_upload('imgFile'))
		{
			$this->alert($this->upload->get_error());
		}
		
		$data = $this->upload->get_data();
		echo json_encode(array('error' => 0, 'url' => $file_url));
		exit();
	}
	
	public function manage()
	{
		//根据参数确定子目录
		$dir = $_GET['dir'];
		if ( ! in_array($dir, array('image', 'flash', 'media' , 'file')))
		{
			$this->alert('目录类型错误');
		}
		
		//确定文件后缀名
		switch ($dir)
		{
			case 'image':
				$ext_arr = array('gif', 'jpg', 'jpeg', 'png', 'bmp');
				break;
			case 'flash':
				$ext_arr = array('flv', 'swf');
				break;
			case 'media':
				$ext_arr = array('swf', 'flv', 'mp3', 'wav', 'wma', 'wmv', 'mid', 'avi', 'mpg', 'asf', 'rm', 'rmvb');
				break;
			case 'file':
				$ext_arr = array('doc', 'docx', 'xls', 'xlsx', 'ppt', 'htm', 'html', 'txt', 'zip', 'rar', 'gz', 'bz2');
				break;
		}
		
		$path = $this->path.'/'.$dir;
		
		$_GET['path'] = (isset($_GET['path']) && trim($_GET['path']) != '') ? ('/'.trim($_GET['path'])) : '';
		
		//根据path参数，设置各路径和URL
		if ($_GET['path'] == '')
		{
			$current_path = $path;
			$current_url = $root_url;
			$current_dir_path = '';
			$moveup_dir_path = '';
		}
		else
		{
			$current_path = $path.$_GET['path'];
			$current_url = '/codeigniter/uploads/image'.$_GET['path'].'/';
			$current_dir_path = $_GET['path'];
			$moveup_dir_path = preg_replace('/(.*?)[^\/]+\/$/', '$1', $current_dir_path);
		}
		
		//不允许使用..移动到上一级目录
		if (preg_match('/\.\./', $current_path))
		{
			echo '没有权限访问';
			exit;
		}
		
		//遍历目录取得文件信息
		$file_list = array();
		if ($handle = opendir($current_path))
		{
			$i = 0;
			while (false !== ($filename = readdir($handle))) {
				if ($filename{0} == '.') continue;
				$file = $current_path.'/'.$filename;
				if (is_dir($file)) {
					$file_list[$i]['is_dir'] = true; //是否文件夹
					$file_list[$i]['has_file'] = (count(scandir($file)) > 2); //文件夹是否包含文件
					$file_list[$i]['filesize'] = 0; //文件大小
					$file_list[$i]['is_photo'] = false; //是否图片
					$file_list[$i]['filetype'] = ''; //文件类别，用扩展名判断
				} else {
					$file_list[$i]['is_dir'] = false;
					$file_list[$i]['has_file'] = false;
					$file_list[$i]['filesize'] = filesize($file);
					$file_list[$i]['dir_path'] = '';
					$file_ext = strtolower(array_pop(explode('.', trim($file))));
					$file_list[$i]['is_photo'] = in_array($file_ext, $ext_arr);
					$file_list[$i]['filetype'] = $file_ext;
				}
				$file_list[$i]['filename'] = $filename; //文件名，包含扩展名
				$file_list[$i]['datetime'] = date('Y-m-d H:i:s', filemtime($file)); //文件最后修改时间
				$i++;
			}
			closedir($handle);
		}
		
		$result = array();
		//相对于根目录的上一级目录
		$result['moveup_dir_path'] = $moveup_dir_path;
		//相对于根目录的当前目录
		$result['current_dir_path'] = $current_dir_path;
		//当前目录的URL
		$result['current_url'] = $current_url;
		//文件数
		$result['total_count'] = count($file_list);
		//文件列表数组
		$result['file_list'] = $file_list;
		
		//输出JSON字符串
		//header('Content-type: application/json; charset=UTF-8');
		echo json_encode($result);
		exit();
	}
	
	public function alert($msg) 
	{
		header('Content-type: text/html; charset=UTF-8');
		echo json_encode(array('error' => 1, 'message' => $msg));
		exit();
	}

}